package org.wattdepot.cli;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.util.tstamp.Tstamp;
//import org.wattdepot.resource.source.jaxb.Property;
import java.util.List;

/**
 * Prints a table listing all of the sensor data instances for the specified day.
 * @author Edwin Lim, Wing Cheung Ma
 * 
 */
public class SensorDataDay extends CommandLineInterface implements WattDepotCliCommand {
  /**
   * Gets input from the user and finds the sensor data according to the time instance.
   * @param args with all the inputs
   * @return a string with the daily sensor data.
   */
  @Override
  public String doCommand(List<String> args) {
     
    String sourceName = args.get(0);
    String day = args.get(1);
    String properties = "";
    XMLGregorianCalendar startTime = null;
    XMLGregorianCalendar endTime = null;
    List<SensorData> sensorDataList = null;
    
    Source source = null;
    try {
      source = searchSource(sourceName);
    }
    catch (WattDepotClientException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    
    if (source == null) {
      return sourceNotFound;
    }
    
    try {
      startTime = Tstamp.makeTimestamp(day);
      endTime = Tstamp.incrementDays(startTime, 1);
    }
    catch (Exception e) {
      System.out.println("should never happen.");
      return timeProblem;
    }
       
    try {
      try {
        sensorDataList = client.getSensorDatas(sourceName, startTime, endTime);
      }
      catch (NotAuthorizedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (BadXmlException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (MiscClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    catch (ResourceNotFoundException notFound) {
      System.out.println("should never happen.");
      return sourceNotFound;
    }
    
    try {
      sensorDataList = client.getSensorDatas(sourceName, startTime, endTime);
    }
    catch (NotAuthorizedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (ResourceNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (BadXmlException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    for (SensorData sensorData : sensorDataList) {
      StringBuffer sb = new StringBuffer(255);
      
      if (source.isSetProperties()) {
        for (org.wattdepot.resource.property.jaxb.Property property : 
          sensorData.getProperties().getProperty()) {
          sb.append(" [");
          sb.append(property.getKey());
          sb.append(", ");
          Double power = Double.parseDouble(property.getValue());
          sb.append(power);
          sb.append("] ");
        }
        //properties = sb.toString();
      }
      else {
        sb.append("none");
      }
      properties = sb.toString();
      properties = sensorData.getTimestamp() + "\t" + " Tools: " + sensorData.getTool() + 
          "Properties: (" + properties + ")";
    }
    return properties;
  }
}
